﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApXMLFileWriter
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Text;
using System.Xml;


namespace DnProcessSimulateCommands;

internal abstract class CApXMLFileWriter
{
  protected string m_filePath;
  protected TxExportGraphicsAnimationOutputData m_animationOutputData;
  protected XmlDocument m_document = new XmlDocument();
  protected XmlElement m_rootElement;
  protected CApC3DDocumentationSettings m_documentationSettings;

  internal CApXMLFileWriter(string filePath) => this.m_filePath = filePath;

  internal virtual void Initialize(
    TxExportGraphicsAnimationOutputData animationOutputData,
    CApC3DDocumentationSettings documentationSettings)
  {
    this.m_animationOutputData = animationOutputData;
    this.m_documentationSettings = documentationSettings;
    XmlDeclaration xmlDeclaration = this.m_document.CreateXmlDeclaration("1.0", (string) null, (string) null);
    xmlDeclaration.Encoding = Encoding.UTF8.HeaderName;
    this.m_document.AppendChild((XmlNode) xmlDeclaration);
  }

  internal void Close() => this.m_document.Save(this.m_filePath);

  protected XmlElement AddChild(XmlElement parent, string elementName)
  {
    XmlElement element = this.m_document.CreateElement(elementName);
    parent.AppendChild((XmlNode) element);
    return element;
  }

  protected XmlElement AddUniqueChild(XmlElement parentElement, string elementName, string comment)
  {
    XmlElement xmlElement = parentElement[elementName];
    if (xmlElement == null)
    {
      XmlNode node = this.m_document.CreateNode(XmlNodeType.Comment, "", "");
      node.Value = comment;
      parentElement.AppendChild(node);
      xmlElement = this.AddChild(parentElement, elementName);
    }
    return xmlElement;
  }

  protected XmlElement AddUniqueChild(XmlElement parentElement, string elementName)
  {
    return parentElement[elementName] ?? this.AddChild(parentElement, elementName);
  }

  protected void SetUniqueChildValue(
    XmlElement parentElement,
    string elementName,
    string comment,
    string value)
  {
    this.AddUniqueChild(parentElement, elementName, comment).InnerText = value;
  }

  protected void SetUniqueChildValue(XmlElement parentElement, string elementName, string value)
  {
    this.AddUniqueChild(parentElement, elementName).InnerText = value;
  }

  protected void SetChildValue(
    XmlElement parent,
    string elementName,
    string value,
    bool createAlways)
  {
    XmlElement xmlElement = parent[elementName];
    if (xmlElement == null | createAlways)
      xmlElement = this.AddChild(parent, elementName);
    xmlElement.InnerText = value;
  }

  protected XmlElement SetUniqueChildAttribute(
    XmlElement parent,
    string elementName,
    string attributeName,
    string attributeValue)
  {
    XmlElement xmlElement = this.FindElement(parent, elementName, attributeName, attributeValue);
    if (xmlElement == null)
    {
      xmlElement = this.AddChild(parent, elementName);
      xmlElement.SetAttribute(attributeName, attributeValue);
    }
    return xmlElement;
  }

  protected XmlElement FindElement(
    XmlElement parent,
    string elementName,
    string attributeName,
    string attributeValue)
  {
    string xpath = $"{elementName}[@{attributeName}='{attributeValue}']";
    return parent.SelectSingleNode(xpath) as XmlElement;
  }
}
